﻿using System;
using System.Windows.Forms;
using System.Xml;
namespace Member.Client
{
    public class Common : Form
    {
        public static string Url = "http://localhost:12431/";
        public static string apk_url = "http://openboxcdn.mobilem.360.cn/mintf/getAppInfoByIds?pname=";
        /// <summary>
        /// 关闭当前程序
        /// </summary>
        /// <param name="name"></param>
        public static void Close(string name)
        {
            ChangeStatus(name, 0);
            Application.ExitThread();
        }
        /// <summary>
        /// 更改状态
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        public static void ChangeStatus(string name, int status)
        {
            Net.NetCommon.GetCode(Url + "/Api/?action=ChangeStatus&name=" + name + "&status=" + status);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="name"></param>
        /// <param name="passwd"></param>
        /// <returns></returns>
        public static string Login(string name, string passwd)
        {
            return Net.NetCommon.GetCode(Common.Url + "/Api/?action=login&name=" + name.Trim() + "&passwd=" + passwd.Trim());
        }

        /// <summary>
        /// 得到用户实体
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Model.Users GetModel(XmlDocument xml)
        {
            Model.Users model = new Model.Users();
            try
            {
                model.ID = int.Parse(xml.SelectSingleNode("content/ID").InnerText);
                model.Name = xml.SelectSingleNode("content/Name").InnerText;
                model.Passwd = xml.SelectSingleNode("content/Passwd").InnerText;
                model.Email = xml.SelectSingleNode("content/Email").InnerText;
                model.Phone = xml.SelectSingleNode("content/Phone").InnerText;
                model.Type = int.Parse(xml.SelectSingleNode("content/Type").InnerText);
                model.Product = xml.SelectSingleNode("content/Product").InnerText;
                model.RegTime = DateTime.Parse(xml.SelectSingleNode("content/RegTime").InnerText);
                model.RegIP = xml.SelectSingleNode("content/RegIP").InnerText;
                model.LoginTime = DateTime.Parse(xml.SelectSingleNode("content/RegTime").InnerText);
                model.LoginIp = xml.SelectSingleNode("content/RegIP").InnerText;
                model.Session = xml.SelectSingleNode("content/Session").InnerText;
                model.PicTure = xml.SelectSingleNode("content/PicTure").InnerText;
                model.StartTime = DateTime.Parse(xml.SelectSingleNode("content/StartTime").InnerText);
                model.EndTiem = DateTime.Parse(xml.SelectSingleNode("content/EndTiem").InnerText);
                model.Status = int.Parse(xml.SelectSingleNode("content/Status").InnerText);
            }
            catch { model = null; }
            return model;
        }
        /// <summary>
        /// 获取会员级别
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetLevel(int type)
        {
            switch (type)
            {
                case 0:
                    return "受限会员";
                case 1:
                    return "普通会员";
                case 2:
                    return "高级会员";
                case 3:
                    return "金牌会员";
                case 10:
                    return "管理员";
                default:
                    return "未知?";
            }
        }

        /// <summary>
        /// 获取软件所有字段
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Model.Soft GetModel(string str,int count,int time)
        {
            str = str.Replace(":[{", ":{").Replace("}]}", "}}");
            Model.Soft model = new Model.Soft();
            Json.datajson dj = Json.JsonHelper.JsonDeserialize<Json.datajson>(str);
            model.Apkid=dj.data.apkid;
            model.Apk_md5 = dj.data.apk_md5;
            model.Corp = dj.data.corp;
            model.Down_url = dj.data.down_url;
            model.Download_times = long.Parse(dj.data.download_times);
            model.ID = int.Parse(dj.data.id);
            model.Name = dj.data.name;
            model.Signature_md5 = dj.data.signature_md5;
            model.Version_name = dj.data.version_name;
            model.Count = count;
            model.Time = time;
            return model;
        }

    }

}
